package com.alumni_circle.service.security.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alumni_circle.core.config.RedisCache;
import com.alumni_circle.core.entity.rbac.LoginUser;
import com.alumni_circle.core.service.Impl.BaseServiceImpl;
import com.alumni_circle.entity.me.User;
import com.alumni_circle.entity.security.UserSecurity;
import com.alumni_circle.mapper.me.UserMapper;
import com.alumni_circle.mapper.security.UserSecurityMapper;
import com.alumni_circle.service.security.IUserSecurityService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @author 龙小虬
 * @since 2020-07-29 13:42
 */

@Service
public class UserSecurityServiceImpl extends BaseServiceImpl<UserSecurityMapper, UserSecurity> implements IUserSecurityService {

    @Resource
    UserMapper userMapper;

    @Resource
    RedisCache redisCache;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.getUserInfo(username);
        if(!ObjectUtil.hasEmpty(user)) {
            return createLoginUser(user, redisCache.getCacheSet(user.getRole()));
        }
        return null;
    }

    public static UserDetails createLoginUser(User user, Set set){

        return new LoginUser(user,set);
    }
}
